package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.Admin;
import com.example.cdctest.pojo.Notice;
import com.example.cdctest.service.NoticeService;
import com.example.cdctest.util.JSONResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/manage/notice")
public class NoticeController {

    protected static Log logger = LogFactory.getLog(NoticeController.class);

    @Resource
    private NoticeService noticeService;

    //转到公告列表页面
    @RequestMapping("notice-list")
    @ModelAttribute("noticeList")
    public List<Notice> noticeList(){
        return noticeService.findAll();
    }

    @RequestMapping("notice-add")
    public void noticeAdd(){ }

    /**
     * @author qinxing
     * @Description 图片上传
     * @date 2020/10/12 17:36
     * @param multipartFile, request
     * @return net.dengnihuijia.pojo.ResultVO
     */
    @RequestMapping("rest/upload")
    @ResponseBody
    public JSONResult upImg(@RequestParam("edit") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        String substring = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String path = request.getServletContext().getRealPath("/static/forumImg");
        //新的文件名称
        String newFileName = UUID.randomUUID().toString() + substring;
        multipartFile.transferTo(new File(path,newFileName));
        JSONResult jsonResult=new JSONResult();
        jsonResult.setStatus(0);
        jsonResult.setMsg("ok");
        // data 数据为 文件的下载路径
        jsonResult.setData("/static/forumImg/" + newFileName);
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("lunBo/add")
    public JSONResult addlunbo(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String substring = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = request.getServletContext().getRealPath("/static/forumImg");
        //新的文件名称
        String newFileName = UUID.randomUUID().toString() + substring;
        file.transferTo(new File(path,newFileName));
        JSONResult jsonResult=new JSONResult();
        jsonResult.setStatus(0);
        jsonResult.setMsg("ok");
        // data 数据为 文件的下载路径
        jsonResult.setData("/static/forumImg/" + newFileName);
        return jsonResult;
    }

    //根据公告信息的标题,公告信息的封面,具体的公告信息添加公告
    @RequestMapping("insert-notice-message")
    @ResponseBody
    public String insertNoticeMessage(String title,String content,String image,HttpServletRequest request){
//         Admin loginAdmin=(Admin)request.getSession().getAttribute("loginAdmin");
        Admin admin=new Admin();
        admin.setId(1);
        noticeService.insertNoticeMessage(title,content,image,admin);
        return "redirect:/manage/index";
    }

    //删除公告信息
    @RequestMapping("delete-notice")
    @ResponseBody
    public void deleteNotice(Integer id){
        noticeService.delete(id);
    }

    //修改公告信息排序
    @RequestMapping("update-sort")
    @ResponseBody
    public void updateNoticeSort(Notice notice){
        noticeService.update(notice);
    }
}
